So now that I have my badge created in Roblox, I want to be able to award it to players when they first
triggered the boulder trap.
And this is my boulder trap right here.
Trigger It is the script that actually triggers the boulder trap to go.
So that's what I want.
I'm going to open my trigger now.
I need a variable for the badge ID I created.
I'm going to call this beat for Boulder Trap Badge ID If you don't have the badge ID, we could go to
the website.
This is from the last video.
Hit those three dots for your badge that you created.
Copy Asset ID.
Go back to your code control V.
There you go.
I said ID of the badge.
Now we also need the badge service, so I'm going to call that B service for bad service as a game.
Get service, Badge service.
So this badge service is going to be in this variable right here.
All right.
Now I'm going to make a function local function.
I'm going to call this award badge and I'm going to make this general so that if I want to do badges
elsewhere, I could just copy and paste it.
I'm going to pass in a player.
The person is going to get the badge, I'm going to pass in a variable for the badge ID, but I'm not
going to make it specific to this badge because I might want to use this code elsewhere.
I'm going to make a variable, a Boolean variable that's has badge and we'll initialize it to false
because we don't want to award it more than once.
And then I'm going to use an asynchronous call to Roblox to see if the person actually got the badge.
But I'm going to wrap that in something called the P call, a protected call.
It's kind of a weird pattern.
It'll take a while to get used to, right?
It took me a while to get used to and I was a programmer already.
So we'll do this local.
We'll declare two variables.
It's going to be success and message, and this is what we're going to get back from the p call, right?
Just like that.
And then I'm going to put an anonymous function inside the call.
So we are protecting this.
A protected call is going to give us status on how that asynchronous call went.
So like if your brother kicks out the Internet cable or something, you will get the error and message
is going to tell you.
You're going to give you a hint of what it is success or failure is going to be, whether it went through
or there's a problem detected from the p call.
It's kind of clever.
So what we're going to do is we're going get this has badge boolean and set it equal to the badge services
result from user has badge async call.
Right.
We need to pass in the players user ID and the badge ID and that's going to go to roadblocks and say,
Hey, does this guy have the badge?
If he does, it's true.
If he doesn't, if he does, if it's if he doesn't, it's false.
Right.
But we're also going to check to make sure that we didn't run into problems here.
So we've got to do some sort of statement like, if not success, the recall.
Notice that there is an error.
Maybe the Internet went out or something went to say warn, this will be for troubleshooting.
Something went wrong.
What's wrong with the call?
Well, let's do with the with the badge call and then we can put the message right here.
Right.
And then what we're going to do is we're just going to return, right?
So something went wrong.
They didn't get there.
We're not going to give them the badge, but we'll just leave right there.
And that way they can always get it again, right.
I'm not going to do a loop or anything just to make sure it's it's overkill, I think, for the badge.
So we'll say if the if has badge, let's say if has badge equals false, Right.
They don't have the badge.
How else can we write that.
Lots of ways.
Probably a better way.
You can leave it like this if you want.
I'm just going to show you another way.
You could say if not has badge right two ways.
Then we're going to say B service.
We're going to award the badge.
There it is, award badge.
We'll pass in the player, the player user ID, right, The player user ID, and then we're going to
get our badge number.
What is our badge number?
The badge ID, I set it wrong.
Passing the badge ID that should do it.
We could put a little troubleshooting in here like, Well, let's just go ahead and do an LS.
Right?
Else they do have a badge.
We'll put a little message just so we know we're touching it.
Right.
You have the badge.
That's pretty cool.
All right.
That's what.
That's all you need.
Let's go ahead and copy this.
Right.
We're going to copy that, Control.
See, we're going to go down to our on touch.
And then when we successfully trigger play the boulder sound, we'll just go ahead and check that.
Oh, but we need a player.
We do have a badge number, right?
We have the badge number.
We have the BT Badge ID, but we don't have a player.
Let's get the player.
We've got a humanoid.
If you get a humanoid, you can get a player.
Well, unless there's no player because Zombie might might trip it too.
So you don't want to give it to a zombie.
We better check.
We better check to see if there's a player.
So local player.
Well, if this is a humanoid, then this would be the character, Right?
So.
Let's get our prayer service.
We can do this game, players.
That gives us the player service and then colon get player from character and other part dot parent
would be the character if the humanoid did exist.
I'm going to put this on a second line so you could see it.
You could leave it all on one line if you want, right?
But that way you can see it.
All right.
So we theoretically have a player.
We're not sure, though.
It could have been a zombie.
Zombies have humanoids.
We got to make sure.
So we're say if player right, if the player was returned from the player service, then we will call
our ward badge and not otherwise we're good to go.
We should be able to get this badge.
Let's go ahead and play this game, see what we got.
It should pop up down here.
We go.
Looking good.
Let's do it.
There it is.
We got the badge.
You just missed that boat or two.
All right, so now you know how to add badges to your game.
And the next video.
I'm going to just show you how to delete it from your account.
So if you test this, you don't have to make, like, 20 badges, right?
Because you don't want to you don't have to get a new one every time you test.